<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>

<script type="text/javascript">
    var rowId = 0;
    $('fbox_requestGrid .ui-reset , #refresh_requestGrid').click(function(){
        $("#requestGrid").jqGrid('setGridParam',{search:false});
        var postData = $("#requestGrid").jqGrid('getGridParam','postData');
        $.extend(postData,{searchField:"",searchString:"",searchOper:""});
        $.extend(postData,{filters:""});
        $("#requestGrid").trigger("reloadGrid");
    });

    $.subscribe('searchrequestGrid', function(event,data) {
        $("#requestGrid").jqGrid('searchGrid', {closeAfterSearch:true,closeOnEscape:true,sopt:['eq'],caption:"Search request"} );
    });
    
    $.subscribe('requestGridRowSelected', function(event,data) {
        rowId = event.originalEvent.id;
        $( "#requestGrid_delete" ).button( "option", "disabled", false );
    });
   
    $.subscribe('deleterequestGrid',function(event,data){
        if(rowId!=0){
            $("#requestGrid").jqGrid('delGridRow',rowId,{resize:true,drag:true,closeOnEscape:true,closeAfterEdit:true,jqModal:true,left:310,top:20,reloadAfterSubmit:false,afterSubmit:function(response, postdata) {return isError(response.responseText);}});
        }else{
            alert("Sorry. Please select a product first.");
        }
    });
    
    $.subscribe('requestGridComplete',function(event,data){
        $( "#requestGrid_search" ).button( "option", "disabled", false );
    });
    
    
    function isError(text) {
        if(text.indexOf('ERROR') >= 0) { return [false, text]; }
        return [true,''];
    }

</script>
<div class="gridOptsContainer">
    <sj:submit button="true" cssStyle="font-size:14px;" disabled="true" id="requestGrid_search" onClickTopics="searchrequestGrid" value="Search request"/>
    <sj:submit button="true" cssStyle="font-size:14px" disabled="true" id="requestGrid_delete" onClickTopics="deleterequestGrid" value="Delete request"/> 
</div>
<s:url id="remoteurl" action="grid-request"/>
<s:url id="editurl" action="del-request"/>

<sjg:grid
    id="requestGrid"
    caption="Cashmate Request"
    dataType="json"
    href="%{remoteurl}"
    editurl="%{editurl}"
    gridModel="gridModel"
    groupField="['branchname']"
    groupColumnShow="[true]"
    groupCollapse="false"
    loadingText="Requesting Data..."
    width="970"
    shrinkToFit="true"
    pager="true"
    pagerButtons="false"
    pagerInput="false"
    hoverrows="true"
    rownumbers="true"
    autoencode="true"
    viewrecords="true"
    navigator="true"
    navigatorAdd="false"
    navigatorDelete="false"
    navigatorEdit="false"
    navigatorSearch="true"
    navigatorSearchOptions="{closeAfterSearch:true,closeOnEscape:true,sopt:['eq'],caption:'Search Request'}"
    navigatorView="true"
    onSelectRowTopics="requestGridRowSelected"
    onGridCompleteTopics="requestGridComplete"
    >

    <sjg:gridColumn name="requestid"
                    index="requestid"
                    title="Request ID"
                    sortable="false"
                    key="true"
                    hidden="true"
                    />


    <sjg:gridColumn name="branchname"
                    index="branchname"
                    title="Branch"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="requestdate"
                    index="requestdate"
                    title="Request Date"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="documents.documentno"
                    index="documents.documentno"
                    title="Document No."
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="documents.documenttype"
                    index="documents.documenttype"
                    title="Document Type"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="employees.completename"
                    index="employees.completename"
                    title="Employee"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="status"
                    index="status"
                    title="Status"
                    sortable="true"
                    align="center"
                    />


</sjg:grid>